define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Groupbuy_goods, $ionicSlideBoxDelegate, GoPage, GoDialog, GoShare,GoWapAuth, $window, $timeout, $stateParams, $ionicScrollDelegate, Auth, $interval,$ionicPopup) {
		//一键分享
		$scope.isHide = true; //默认隐藏
		$scope.goods_id = $stateParams.goods_id;
		$scope.product_id = $stateParams.product_id;
		var goods_img = new Array();
		$scope.input = new Object();
		var mktprice = '';
		var suit_goods_count = 0;
		var l = 0;
		var mktprice = '';
		var store = '';
		var price = 0;
		var product = new Array();
		var adjunct = new Array();
		$scope.act_id = $stateParams.act_id;
		$scope.is_price = true;
		$scope.group_rule = null;
		$scope.isshow = true;
		$scope.btn_name = '';
		$scope.minimum = 1;
		$scope.set_time = false;
		$scope.favorite = false;
		$scope.$watch('input.goods_num', function(newValue, oldValue) {
				if(newValue == oldValue) return;
				var min_buy = 1;
				if($scope.detail.min_buy != null && $scope.detail.min_buy != '0')
					min_buy = $scope.detail.min_buy;
				if($scope.detail.max_count != undefined)
					var max_count = $scope.detail.max_count > $scope.detail.store ? $scope.detail.store : $scope.detail.max_count;
				else
					var max_count = $scope.detail.store;
				if(newValue > max_count) {
					$scope.input.goods_num = parseInt(max_count);
					return;
				}
				if(newValue == undefined) {
					$scope.input.goods_num = min_buy;
					return;
				}

			})
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.init_data();
			$scope.set_time = false;
		}, false);
		//页面离开，分享关闭
		$scope.$on('$ionicView.afterLeave', function() {
			$scope.closeGuidShare();
		}, false);
		$scope.$on('$ionicView.beforeLeave',function(){
			if($scope.timer){
				$interval.cancel($scope.timer);
			}
		})
			//显示弹窗
		$scope.show_apply = function() {
			if($scope.openGuidShare()){
				$scope.isHide = false;
			}
		};
		$scope.hide_apply = function() {
			$scope.isHide = true;
		};
		$scope.onshow_first = function(store_id) {
			GoPage.go_states("first", {
				"store_id": store_id
			});
		};
		$scope.onshow_afsrue = function(type, status) {
			GoPage.go_states("afsrue", {
				"type": type,
				"status": status
			});
		};
		$scope.go_Panicbuying_goods = function(goods_id) {
			GoPage.go_states("panicbuying_goods", {
				"goods_id": goods_id,
				"type": $scope.act_id
			});
		};
		$scope.onshow_productInfo = function(index, goods_id) {
			GoPage.go_states("groupbuy_pinfo", {
				"id": index,
				"isTransfer": false,
				"goods_id": goods_id,
				"act_id": $scope.act_id,
				"product_id": $scope.product_id
			});
		}
		$scope.onshow_tabCart = function() {
			GoPage.go_states("tab_cart");
		};
		$scope.goods_add = function() {
			var min_buy = 1;
			if($scope.detail.min_buy != null && $scope.detail.min_buy != '0')
				min_buy = $scope.detail.min_buy;
			$scope.input.goods_num += parseInt(min_buy);
			if($scope.detail.max_count != undefined)
				var max_count = $scope.detail.max_count > $scope.detail.store ? $scope.detail.store : $scope.detail.max_count;
			else
				var max_count = $scope.detail.store;
			if($scope.input.goods_num > max_count)
				$scope.input.goods_num = parseInt(max_count);
		};
		$scope.goods_reduce = function() {
			var min_buy = 1;
			if($scope.detail.min_buy != null && $scope.detail.min_buy != '0')
				min_buy = $scope.detail.min_buy;
			if($scope.input.goods_num > $scope.minimum)
				$scope.input.goods_num -= parseInt(min_buy);

		};
		$scope.get_product = function(type) {
			product = new Array();
			adjunct = new Array();
			if($scope.detail.store == 0) {
				GoDialog.toast("当前选中的" + $scope.detail.name + "没有库存了");
				return;
			}
			if(type != 'coupon' && type != 'package') {
				//检测是否选有所有规格
//				if($scope.detail.specification.length != 0) {
//					for(var i = 0; i < $scope.detail.specification.length; i++) {
//						if($scope.detail.specification[i].checked != true) {
//							GoDialog.toast("请为商品的" + $scope.detail.specification[i].name + "选值");
//							return;
//						}
//					}
//
//				} else {
//					$scope.product_id = $scope.detail.goods_specification[0].product_id;
//				}
				var i = product.length;
				product[i] = {
					"product_id": $scope.product_id,
				}
			}
			$scope.num = $scope.input.goods_num;
			if($scope.num == undefined) {
				GoDialog.toast("请填写购买数量");
				return;
			}
			if($scope.num == 0) {
				GoDialog.toast("请填写购买数量");
				return;
			}
			Groupbuy_goods.buy($scope.detail.goods_id, $scope.product_id, $scope.num, $scope.act_id, function(data) {
				if(data.success == true)
					$scope.onshow_afsrue('groupbuy', 'fastbuy');
				else {
					GoDialog.toast(data.msg);
					$scope.init_data();
				}
			})
		}
		
		$scope.fast_buy = function() {
			$scope.status = 'fastbuy';
			$scope.get_product('timebuy');
		}
		$scope.init_is_price = function() {
			if($scope.detail.goods_specification.length == 1)
				$scope.is_price = true;
			else
				$scope.is_price = false;
		}
		$scope.init_buy_num = function() {
//			if($scope.detail.min_buy != null && $scope.detail.min_buy != '0'){
//				if($scope.minimum!=1){
//					$scope.input.goods_num = $scope.minimum;
//				}else{
//					$scope.input.goods_num = parseInt($scope.detail.min_buy);
//				}
//			}
//			else
//				$scope.input.goods_num = 1;
			if($scope.detail.min_buy!=null&&$scope.detail.min_buy != '0'){
//				if($scope.detail.min_buy>=$scope.minimum){
//					$scope.input.goods_num = $scope.detail.min_buy;
//				}else{
					if($scope.minimum%$scope.detail.min_buy==0){
						$scope.input.goods_num = $scope.minimum;
					}else{
						$scope.input.goods_num = (parseInt($scope.minimum/$scope.detail.min_buy) +1)*$scope.detail.min_buy;
					}
//				}
			}else{
				$scope.input.goods_num = $scope.minimum;
			}
			
//			if($scope.detail.goods_specification.length == 1) {
//				if($scope.detail.specification.length != 0) {
//					if($scope.detail.specification[0].detail) {
//						if($scope.detail.specification[0].detail.length < 1) {
//							$scope.detail.max_count = $scope.detail.goods_specification[0].max_count;
//						}
//					} else {
//						$scope.detail.max_count = $scope.detail.goods_specification[0].max_count;
//					}
//
//				} else {
//					$scope.detail.max_count = $scope.detail.goods_specification[0].max_count;
//				}
//			}
		};
		$scope.init_num = function() {
			if($scope.detail.mktprice != undefined)
				mktprice = $scope.detail.mktprice;
			if($scope.detail.store != undefined)
				store = $scope.detail.store;
			if($scope.detail.price != undefined)
				price = $scope.detail.price;
			if($scope.detail.specification != undefined)
				l = $scope.detail.specification.length;
			//				if($scope.input.goods_num>$scope.detail.store)
			//			    $scope.input.goods_num = parseFloat($scope.detail.store);
		};
		$scope.init_handle = function() {
			$ionicSlideBoxDelegate.$getByHandle('advs-handle').update(); //解决无法加载
			$ionicSlideBoxDelegate.$getByHandle('advs-handle').loop(true); //循环
			$scope.style = "shop_grade" + " " + "grade" + $scope.detail.store_level;
			if($scope.detail.related_goods == undefined) {
				$scope.selectTab = 0
					//				$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
			} else if($scope.detail.group == undefined) {
				$scope.selectTab = 1;
				//				$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
			}
		};
		$scope.init_time = function(data, now_time) {
			if(now_time < data.act_start_time) {
				$scope.isshow = false;
				$scope.btn_name = '未开始';
				$scope.act_time_name = '距开始';
				if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else
					data.now_time = data.act_start_time - now_time;
			} else {
				$scope.act_time_name = '距结束';
				if($scope.detail.store <= 0) {
					$scope.btn_name = '已售完';
					$scope.isshow = false;
				}
				if(data.now_time != undefined)
					data.now_time = data.now_time - 1;
				else
					data.now_time = data.act_end_time - now_time;

			}
			data.formate_time = $scope.formate_time(data.now_time);
			$scope.set_timer(data.process_time);
		};
		$scope.set_timer = function(now_time) {
			if(!$scope.set_time){
				$scope.set_time = true;
			}else{
				return;
			}
			$scope.timer =  $interval(function() {
				if($scope.detail.now_time != undefined) {
					$scope.detail.now_time = $scope.detail.now_time - 1;
					if($scope.detail.now_time <= 0) {
						if(now_time >= $scope.detail.act_start_time) {
							$scope.detail.now_time = $scope.detail.act_end_time - now_time;
							$scope.act_time_name = '距结束';
							if($scope.detail.store <= 0) {
								$scope.btn_name = '已售完';
								$scope.isshow = false;
							} else {
								$scope.isshow = true;
							}
						}
					}
				} else {
					if(now_time > $scope.detail.act_start_time) {
						$scope.detail.now_time = $scope.detail.act_end_time - now_time;
						$scope.act_time_name = '距结束';
						if($scope.detail.store <= 0) {
							$scope.btn_name = '已售完';
							$scope.isshow = false;
						} else {
							$scope.isshow = true;
						}
					} else {
						$scope.detail.now_time = $socpe.detail.act_start_time - now_time;
						$scope.act_time_name = '距开始';
					}
				}
				now_time++;
				$scope.detail.formate_time = $scope.formate_time($scope.detail.now_time);
			}, 1000)
		};
		$scope.formate_time = function(time) {
			if(time > 0) {
				var day = parseInt(time / (3600 * 24));
				var hour = parseInt((time % (3600 * 24)) / 3600);
				if(hour < 10)
					hour = "0" + hour;
				var min = parseInt((time % 3600) / 60);
				if(min < 10)
					min = "0" + min;
				var second = time % 60;
				if(second < 10)
					second = "0" + second;
				return day + '天' + hour + '小时' + min + '分' + second + '秒';
			} else {
				$scope.isshow = false;
				$scope.btn_name = '已结束';
				return '0天0小时0分0秒';
			}
		};
		$scope.init_data = function() {
			$scope.user_id = Auth.get_user_id();
			Groupbuy_goods.product_detail($scope.goods_id, $scope.act_id,$scope.product_id,function(data) {
				if(data.result != null) {
					if(data.name == undefined)
						$scope.msg = '无效商品!';
					goods_img = data.result.title_images;
					$scope.detail = data.result;
//					$scope.product_id = data.result.goods_specification[0].product_id;
					$scope.minimum = data.result.minimum;
					$scope.group_rule = $scope.detail.goods_specification[0].groupbuy_rule;
					$scope.init_buy_num();
					$scope.init_num();
					$scope.init_handle();
					$scope.init_time(data.result, data.process_time);
					$scope.go_share_wx();
					//					$scope.set_timer(data.process_time);
				} else {
					$scope.msg = data.msg;
					$scope.detail = new Object();
				}
			});

		}
		$scope.old_index = 0;
		var comp = new Array();
		//选中的数量
		var is_product = false;
		var is_product_count = 0;
		var spec_count = 0;
		$scope.choose_spec = function(parent_id, value) {
			var count = 0;
			var index1 = '';
			var comp = new Array();
			l = $scope.detail.specification.length;
			//当前组合的组合数组
			for(var i = 0; i < $scope.detail.specification.length; i++) {
				if($scope.detail.specification[i].id == parent_id) {
					for(var n = 0; n < $scope.detail.specification[i].detail.length; n++) {
						if($scope.detail.specification[i].detail[n].value == value) {
							if($scope.detail.specification[i].detail[n].checked == true) {
								$scope.detail.specification[i].detail[n].checked = false;
								$scope.detail.specification[i].checked = false;

							} else {
								$scope.detail.specification[i].detail[n].checked = true;
								$scope.detail.specification[i].checked = true;
								$scope.detail.specification[i].chekced_name = value;
								if($scope.detail.specification[i].detail[n].image != null) {
									var g_image = new Object();
									g_image = $scope.detail.specification[i].detail[n].image;
									//在轮转图中查询当前规格图片的位置
									for(var j = 0; j < $scope.detail.title_images.length; j++) {
										if($scope.detail.title_images[j].image.url == g_image.url) {
											$scope.selectTab = j;
											$ionicSlideBoxDelegate.$getByHandle('advs-handle').slide(j);
											break;
										}
									}
								}
							}
						} else {
							$scope.detail.specification[i].detail[n].checked = false;
						}

					}
				}
			}

			for(var i = 0; i < l; i++) {
				if($scope.detail.specification[i].checked == true)
					count++;
			}
			if(count == l) {
				for(var i = 0; i < $scope.detail.specification.length; i++) {
					var o = comp.length;
					comp[o] = $scope.detail.specification[i].chekced_name;
				}
				for(var i = 0; i < $scope.detail.goods_specification.length; i++) {
					if(comp.toString() == $scope.detail.goods_specification[i].specification_id.toString()) {
						is_product = true;
						var index1 = i;
						break;
					} else {
						is_product = false;
					}

				}
				if(is_product == true) {
					$scope.detail.bn = $scope.detail.goods_specification[index1].bn;
					$scope.product_id = $scope.detail.goods_specification[index1].product_id;
					$scope.detail.max_count = $scope.detail.goods_specification[index1].max_count;
					$scope.group_rule = $scope.detail.goods_specification[index1].groupbuy_rule;
					$scope.product_id = $scope.detail.goods_specification[index1].product_id;
					//					if($scope.detail.goods_specification[index1].act_price != undefined) {
					//						$scope.detail.mktprice = $scope.detail.goods_specification[index1].market_price;
					//						$scope.detail.price = $scope.detail.goods_specification[index1].act_price;
					//					} else {
					//
					//						$scope.detail.price = $scope.detail.goods_specification[index1].market_price;
					//						$scope.detail.mktprice = '';
					//					}

					$scope.detail.store = $scope.detail.goods_specification[index1].store;
					if($scope.detail.max_count != undefined)
						var max_count = $scope.detail.max_count > $scope.detail.store ? $scope.detail.store : $scope.detail.max_count;
					else
						var max_count = $scope.detail.store;
					if($scope.input.goods_num > max_count)
						$scope.input.goods_num = parseInt(max_count);
					$scope.is_price = true;
				} else {
					$scope.group_rule = null;
					$scope.is_price = true;
					$scope.detail.mktprice = mktprice;
					$scope.input.goods_num = 0;
					GoDialog.toast('所选规格没有库存');
				}

			} else {
				$scope.group_rule = null;
				$scope.detail.max_count = null;
				$scope.is_price = true;
				$scope.detail.price = price;
				$scope.detail.mktprice = mktprice;
				$scope.detail.store = store;

			}

		};
		$scope.add_favorite = function(id) {
			if($scope.user_id != '') {
				Groupbuy_goods.add_favorite(id, function(data) {
					if(data.success == true) {
						$scope.init_data();
						GoDialog.toast("商品收藏成功！");

					} else
						GoDialog.toast(data.msg);
				})
			} else {
				GoDialog.toast("请登录后再收藏！");
			}

		};
		$scope.add_favorite_store = function(store_id) {
			if($scope.user_id != '') {
				Groupbuy_goods.add_favorite_store(store_id, function(data) {
					if(data.success == true) {
						$scope.init_data();
						GoDialog.toast("店铺收藏成功！");
					} else
						GoDialog.toast(data.msg);
				})
			} else {
				GoDialog.toast("请登录后再收藏！");
			}
		};
		$scope.do_tel = function(store_id) {
//			if(tel != undefined)
//				$window.location.href = "tel:" + tel;
//			else
//				GoDialog.toast('卖家未留联系方式');
GoPage.go_states('shopcustom',{'store_id':store_id})
		};
		//分享（自定义每个页面的参数）
		$scope.go_share = function(index) {
			var param = new Object();
			if($scope.detail) {
				param.title = $scope.detail.name ? $scope.detail.name : undefined;
				param.desc = $scope.detail.brief ? $scope.detail.brief : undefined;
				param.thumb = $scope.detail.title_images ? $scope.detail.title_images[0].image : undefined;
				param.link = 'groupbuy_goods/'+ $scope.goods_id+'/'+$scope.act_id+'/'+$scope.product_id;
			}
			GoShare.do_share(param, index);
		}
		//分享（自定义每个页面的参数）
		$scope.go_share_wx = function() {
			//分享打开
			GoWapAuth.execisoff(false);
			//设置分享
			var param = new Object();
			if($scope.detail) {
				param.title = $scope.detail.name ? $scope.detail.name : undefined;
				param.desc = $scope.detail.brief ? $scope.detail.brief : undefined;
				param.thumb = $scope.detail.title_images ? $scope.detail.title_images[0].image : undefined;
				param.link = 'groupbuy_goods/'+ $scope.goods_id+'/'+$scope.act_id+'/'+$scope.product_id;
			}
			GoWapAuth.execshare(param);
		}
		
		//客服列表
		var customPopup
		$scope.showCustomList = function(store_id) {
			Groupbuy_goods.contact_show(store_id, function(data) {
				if(data != null){
					$scope.datas = data;
				}else{
					$scope.datas = new Object();
				}
				customPopup = $ionicPopup.show({
					templateUrl: 'templates/modal/customList.html',
					title: '联系客服',
					scope: $scope, //绑定数据，必须写这行
					buttons: [{
						text: "取消",
						type: "button-assertive"
					}]
				});
			})
		}

		//点击其他区域关闭弹出窗       
		var htmlEl = angular.element(document.querySelector('html'));
		htmlEl.on('click', function(event) {　　
			if(event.target.nodeName === 'HTML') {　　　　
				if(myPopup) {　　　　　　
					myPopup.close();　　　　
				}
				if(customPopup) {　　　　　　
					customPopup.close();　　　　
				}
			}
		});
		
		//联系客服
		$scope.do_tel = function(param) {
			customPopup.close();　　　　
			if(param.type == 2) {
				$window.location.href = "tel:" + param.value;
			} else if(param.type == 1) {
				//				GoPage.go_txcall($scope.store_id,$scope.store_id);
				if(param.isonline == '0') {
					GoPage.go_txcall3(param.id, param.code, param.vvckey);
				} else {
					GoPage.go_txcall2($scope.store_id, $scope.store_id, param.id, param.code, param.vvckey);
				}
			}
		}
		
	}

	controller.$inject = ['$scope', 'Groupbuy_goods', '$ionicSlideBoxDelegate', 'GoPage', 'GoDialog', 'GoShare','GoWapAuth', '$window', '$timeout', '$stateParams', '$ionicScrollDelegate', 'Auth', '$interval','$ionicPopup'];

	return app.registerController("groupbuygoodsctrl", controller);
});